#include <iostream>

#include "CacheSearch.h"

#include "ComponentsHolder.h"
#include "EndIO.h"
#include "FlashSearch.h"
//#include "DeviceFetch.h"
//#include "IOStatInfo.h"
#include <iostream>
CacheSearch::CacheSearch(double actionTime, ExtentIO* data): Event(actionTime, data)
{

}


CacheSearch::~CacheSearch(void)
{
}

bool CacheSearch::doAction()
{
	Event * e;
	Cache* cache = ComponentsHolder::getInstance()->getCache();
	EventsHandler* handler = ComponentsHolder::getInstance()->getEventsHandler();
	double actionTime = _actionTime + CacheSearchDelay; // calc action time

//	IOStatInfo* IOinfo=NULL;
//	IOType dataType = _data->getType();
//	if (_data->isReal()){
//		IOinfo = new IOStatInfo();
//		if (dataType == Read)
//			IOinfo->setType(0);
//		else if (dataType == Write)
//			IOinfo->setType(1);
//	}
	
	if (cache->exists(_data))
	{
		ComponentsHolder::getInstance()->getStatisticsHandler()->cacheHit();
		_data->hit(); // mark io as hit
		e = new EndIO(actionTime, _data);
		handler->addEvent(e);	
//		if (_data->isReal()){
			
//			IOinfo->setHit(true);
//			IOinfo->setSequential(_data->isSequential());

//			ComponentsHolder::getInstance()->getAggregatedStatisticsHandler()->updateStat(_data, IOinfo);
//		}
//		int group = ComponentsHolder::getInstance()->getStatisticsHandler()->getPriorityGroup(_data->getLunID());
//		ComponentsHolder::getInstance()->getStatisticsHandler()->increaseGroupIO(group, 0); 
	}
	else
	{
		//cache->increaseIOs();
		
		e = new FlashSearch(actionTime, _data);
		handler->addEvent(e);
//		e = new DeviceFetch(actionTime, _data);	

//		if (_data->isReal()){

//			IOinfo->setHit(false);
//			IOinfo->setSequential(ComponentsHolder::getInstance()->getCache()->isPreviousInCache(_data));

//			ComponentsHolder::getInstance()->getAggregatedStatisticsHandler()->updateStat(_data, IOinfo);
//		}
	}
	return true;
}